/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cassys.ejbs;

import cassys.application.services.ClassGroupManagementBeanRemote;
import cassys.data.ClassGroupDAO;
import cassys.data.DAOFactory;
import cassys.data.ModuleDAO;
import cassys.data.StudentDAO;
import cassys.domain.classManagement.ClassGroup;
import cassys.domain.classManagement.ClassRegistration;
import cassys.domain.classManagement.Module;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;

/**
 *
 * @author User
 */
@Stateless(mappedName = "ClassGroupManagementBean")
public class ClassGroupManagementBean implements ClassGroupManagementBeanRemote {

    DAOFactory daoFactory = DAOFactory.instance(DAOFactory.HIBERNATE);
    ModuleDAO moduleDAO;
    StudentDAO studentDAO;
    ClassGroupDAO classGroupDAO;

    @PostConstruct
    public void init() {
        System.out.println("Post Construct : Assessment Manager Bean");
        moduleDAO = daoFactory.getModuleDAO();
        classGroupDAO = daoFactory.getClassGroupDAO();
        studentDAO = daoFactory.getStudentDAO();
    }

    @Override
    public List<ClassGroup> getAllClassGroups() {
        return classGroupDAO.findAll();
    }

    @Override
    public void createClassGroup(ClassGroup classGroup) {

        try {

            Module module = moduleDAO.findById(classGroup.getModule().getModuleId(), false);
            ClassGroup newClassGroup = new ClassGroup(module, classGroup.getTitle());
            for (ClassRegistration registration : classGroup.getClassMembers()) {
                newClassGroup.addStudent(studentDAO.findById(registration.getStudent().getStudentId(),false));
            }
            classGroupDAO.makePersistent(newClassGroup);
            System.out.println("Persistent done");

        } catch (Exception e) {
            System.out.println("err:" + e.getMessage());
            System.out.println("err:" + e.getStackTrace());
        }
    }

//    private ClassGroup mapNewClassGroup(ClassGroup classGroup) {
//        Module module = moduleDAO.findById(classGroup.getModule(), false);
//        ClassGroup newClassGroup = new ClassGroup(module, classGroup.getTitle());
//
//        long[] students = classGroup.getClassMembers();
//        for (int i = 0; i < students.length; i++) {
//            newClassGroup.addStudent(studentDAO.findById(students[i], false));
//        }
//
//        return newClassGroup;
//    }
    @Override
    public void updateClassGroup(ClassGroup classGroup) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void deleteClassGroup(ClassGroup classGroup) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ClassGroup findByClassGroupId(long classGroupId) {
        return classGroupDAO.findById(classGroupId, false);
    }
}
